package com.example.myapplication;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @auth: njb
 * @date: 2024/1/11 0:18
 * @desc: 描述
 */
public class MainActivity1 extends AppCompatActivity {
    private static final int TOUCH_SLOP = 32; // 触摸滑动的阈值
    private float initialY;
    private boolean isSwiping = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = findViewById(android.R.id.content);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN://下滑退出
                        initialY = event.getY();
                        isSwiping = true;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (isSwiping) {
                            float deltaY = event.getY() - initialY;
                            if (deltaY > TOUCH_SLOP) {
                                // 执行退出应用操作
                                performExitAction();
                                isSwiping = false;
                            }
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        isSwiping = false;
                        break;
                }
                return true;
            }
        });
    }

    private void performExitAction() {
        finish();
    }
}
